package UI;
import Ctr.*;
import Model.*;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Font;
import javax.swing.DropMode;

public class GUI {

	private JFrame frmKassesalg;
	private JTextField textField_1;
	private JTextField textField_2;
	private int vareNummer = 0;
	private double antal = 0;
	private String beskrivelse = "";
	private VareCtr vare = new VareCtr();
	private Vare varer = null;
	private double pris = 0;
	private VareContainer vc;
	private JTextField t = new JTextField();
	private JTextArea textArea = new JTextArea();
	private JLabel lblTotalPris = new JLabel("Total Pris: 0");
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUI window = new GUI();
					window.frmKassesalg.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public GUI() {
		initialize();
		vc = VareContainer.getInstance();
		Vare v = new Vare(204.50, 535.50, 499, 1000, "Boremaskine");
		Vare v1 = new Vare(13, 55, 40, 1001, "Hammer");
		Vare v2 = new Vare(10, 34, 30, 1002, "Skruer");
		Vare v3 = new Vare(175, 500, 400, 1003, "stiksav");
		vc.addVare(v);
		vc.addVare(v1);
		vc.addVare(v2);
		vc.addVare(v3);
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() 
	{
		makeFrame();
		makeTextfields();
		makeButton();
		makeLabels();
		t.selectAll();
	}

	private void makeTextfields()
	{
		t.setText("Indtast Varenummer");
		t.setBounds(167, 161, 178, 22);
		frmKassesalg.getContentPane().add(t);
		t.setColumns(10);
		t.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) 
			{
				int key = e.getKeyCode();
			    if (key == KeyEvent.VK_ENTER) 
			    {
			    	enterButton();
			    }
			}
		});
		
		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setBounds(167, 196, 178, 22);
		frmKassesalg.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(167, 231, 178, 22);
		frmKassesalg.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		textField_2.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) 
			{
				int key = e.getKeyCode();
			    if (key == KeyEvent.VK_ENTER) 
			    {
			    	antalVareButton();
			    }
			}
		});
	}
	
	private void makeButton()
	{
		lblTotalPris.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblTotalPris.setBounds(303, 563, 205, 25);
		frmKassesalg.getContentPane().add(lblTotalPris);
		
		textArea.setEditable(false);
		textArea.setBounds(22, 308, 486, 242);
		textArea.setColumns(2);
		frmKassesalg.getContentPane().add(textArea);
		
		JButton btnFindNummer = new JButton("Find Vare Nummer");
		btnFindNummer.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				enterButton();
			}});
		
		btnFindNummer.setBounds(357, 160, 151, 25);
		frmKassesalg.getContentPane().add(btnFindNummer);
		
		JButton btnNewButton = new JButton("Tilfoej Vare");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				antalVareButton();
			}
		});
		btnNewButton.setBounds(357, 230, 151, 25);
		frmKassesalg.getContentPane().add(btnNewButton);
		
		JButton btnGennemfoer = new JButton("Gennemfoer");
		btnGennemfoer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println(textArea.getText());
				System.out.println(lblTotalPris.getText());
				System.exit(0);
			}
		});
		btnGennemfoer.setBounds(153, 565, 119, 25);
		frmKassesalg.getContentPane().add(btnGennemfoer);
	}
	private void makeLabels()
	{	
		JLabel lblNewLabel = new JLabel("Indtast Varenummer");
		lblNewLabel.setBounds(22, 164, 133, 16);
		frmKassesalg.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Vare Beskrivelse");
		lblNewLabel_1.setBounds(22, 199, 133, 16);
		frmKassesalg.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Antal vare");
		lblNewLabel_2.setBounds(22, 234, 97, 16);
		frmKassesalg.getContentPane().add(lblNewLabel_2);
		
		JLabel lblKvittering = new JLabel("Kvittering");
		lblKvittering.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblKvittering.setBounds(22, 284, 119, 25);
		frmKassesalg.getContentPane().add(lblKvittering);
		
		JButton btnExit = new JButton("Afbryd");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnExit.setBounds(22, 565, 119, 25);
		frmKassesalg.getContentPane().add(btnExit);
		
		JLabel lblNewLabel_3 = new JLabel("");
		lblNewLabel_3.setIcon(new ImageIcon("C:\\Users\\Rasmus\\Dropbox\\gruppe awesome\\SLET IKKE !\\Logo.png"));
		lblNewLabel_3.setBounds(12, 13, 512, 132);
		frmKassesalg.getContentPane().add(lblNewLabel_3);
	}
	private void makeFrame()
	{
		frmKassesalg = new JFrame();
		frmKassesalg.getContentPane().setForeground(Color.WHITE);
		frmKassesalg.setTitle("Kassesalg");
		frmKassesalg.setBounds(100, 100, 555, 655);
		frmKassesalg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmKassesalg.getContentPane().setLayout(null);
	}
	private void enterButton()
	{
		vareNummer = Integer.parseInt(t.getText());
		
		if(vare.findVare(vareNummer) != null)
		{
			varer = vare.findVare(vareNummer);
			pris = varer.getSalgspris();
			beskrivelse = varer.getVareBeskrivelse() + " " + varer.getSalgspris() + " kr.";
			textField_1.setText(beskrivelse);
			textField_2.grabFocus();
			textField_2.selectAll();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Vare Nummer blev ikke fundet");
		}
	}
	private void antalVareButton()
	{
		if(vare.findVare(vareNummer) != null)
		{
			if(textField_2.getText().equals(""))
			{
				textField_2.setText("1");
			}
			String s = Double.toString(pris * Integer.parseInt(textField_2.getText()));
			antal += (pris*Integer.parseInt(textField_2.getText()));
			textArea.append(textField_2.getText() + " x " + varer.getVareBeskrivelse() + "          " + s + " kr.\n");
			lblTotalPris.setText("Total Pris: " + antal + " kr.");
			t.grabFocus();
			t.selectAll();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Vare Nummer blev ikke fundet");
		}
	}
}
